/*********************************************************************************
Organization 					: 				Georgia Institute of Technology
  
Institute 						:				Cognitive Computing Group(CCL)
 
Authors							: 				Santiago Ontanon, Kinshuk Mishra
 												Neha Sugandh 
 												
Class							:				Goal
 
Function						: 				Provides a class for holding the goal
****************************************************************************/
package planninglanguage.goals;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.LinkedList;
import java.util.List;

import org.jdom.Element;

import planninglanguage.conditions.Condition;
import planninglanguage.softconditions.SoftCondition;
import util.XMLWriter;
import base.WargusGameState;

abstract public class Goal implements Cloneable, Serializable{
	
	public static final String XMLtype = "type";
	public static final String XMLplayerID = "player-id";
	
	public Condition generateSuccessCondition(WargusGameState a_gamestate)
	{
		return new Condition();
	}

	public SoftCondition generateSuccessSoftCondition(WargusGameState a_gamestate)
	{
		return new SoftCondition();
	}

	
	public float distance(Goal g) {
		if (!(g instanceof Goal)) return 1;
		return 0;
	}
	abstract public Object clone();
	
	public int unitId()
	{
		return 0;
	}
	
	public String toString() {
		return "Goal()";
	}
	
	public static final String openTag = "goal";
	public static final String closeTag = "/goal";
	abstract public void writeXML(XMLWriter w); /* {
		w.tag(openTag);
		w.tag(closeTag);
	}*/
	
	public static Goal loadGoalFromXML(Element e) {
		assert e.getName().equals("goal") :
			"Goal.loadFromXML: Invalid XML Element " + e.getName();
		
		Goal goal = null;
		
		String goalType = e.getAttributeValue(XMLtype);
		try {
			Method[] methods =
				Class.forName("planninglanguage.goals." + goalType).getDeclaredMethods();
			for (Method m : methods) {
				if (m.getName().equals("loadFromXML")) {
					goal = (Goal) m.invoke(null, new Object[]{e});
					return goal;
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			System.exit(0);
		}
		throw new Error("Goal.loadFromXML: unsupported goal type '" + goalType + "'");
	}
}
